package com.ruicar.afs.cloud.afscase.infomanagement.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.approvetask.entity.WorkTaskPool;
import com.ruicar.afs.cloud.afscase.approvetask.service.WorkTaskPoolService;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.ApproveAssetsChangeInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseBaseInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.mapper.CaseBaseInfoMapper;
import com.ruicar.afs.cloud.afscase.infomanagement.service.ApproveAssetsChangeService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseBaseInfoService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.BusinessStateInEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ProcessTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-05-13 15:20
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class CaseBaseInfoServiceImpl extends ServiceImpl<CaseBaseInfoMapper, CaseBaseInfo> implements CaseBaseInfoService {

    private ApproveAssetsChangeService approveAssetsChangeService;
    private WorkTaskPoolService workTaskPoolService;

    @Override
    public boolean checkNormalFlowEnd(String applyNo,String stageId) {

        BusinessStateInEnum caseStatusEnum = null;

        //资产变更的案件状态取资产变更表中的案件状态
        WorkTaskPool workTaskPool = workTaskPoolService.getOne(Wrappers.<WorkTaskPool>lambdaQuery()
                .eq(WorkTaskPool::getStageId,stageId)
                .eq(WorkTaskPool::getApplyNo,applyNo));

        if (ObjectUtils.isNotEmpty(workTaskPool)) {
            if (ProcessTypeEnum.CHANGE_ASSETS.getCode().equals(workTaskPool.getProcessType())) {
                ApproveAssetsChangeInfo changeInfo = approveAssetsChangeService.getOne(Wrappers.<ApproveAssetsChangeInfo>lambdaQuery()
                        .eq(ApproveAssetsChangeInfo::getApplyNo,applyNo)
                        .eq(ApproveAssetsChangeInfo::getStageId,stageId));
                if (ObjectUtils.isNotEmpty(changeInfo)) {
                    caseStatusEnum = (BusinessStateInEnum) AfsEnumUtil.getEnum(changeInfo.getStatus(),BusinessStateInEnum.class);
                }
            }else {
                CaseBaseInfo baseInfo = Optional.ofNullable(getOne(
                        Wrappers.<CaseBaseInfo>lambdaQuery()
                                .eq(CaseBaseInfo::getApplyNo,applyNo)
                )).orElse(new CaseBaseInfo());
                caseStatusEnum = Objects.isNull(baseInfo.getBusinessStateIn())
                        ? null
                        : (BusinessStateInEnum) AfsEnumUtil.getEnum(baseInfo.getBusinessStateIn(),BusinessStateInEnum.class);
            }
        }
        Set<BusinessStateInEnum> resultBusinessStatus = new HashSet<>();
        resultBusinessStatus.add(BusinessStateInEnum.REPEAL);
        resultBusinessStatus.add(BusinessStateInEnum.REJECT);
        resultBusinessStatus.add(BusinessStateInEnum.APPROVED);
        resultBusinessStatus.add(BusinessStateInEnum.CONDITIONAL_APPROVE);
        resultBusinessStatus.add(BusinessStateInEnum.CANCEL_ADVICE_CONDITIONAL);
        return resultBusinessStatus.contains(caseStatusEnum);
    }

    @Override
    public boolean checkNormalFlowEnd(String applyNo) {
        CaseBaseInfo baseInfo = Optional.ofNullable(getOne(
                Wrappers.<CaseBaseInfo>lambdaQuery()
                        .eq(CaseBaseInfo::getApplyNo,applyNo)
        )).orElse(new CaseBaseInfo());
        BusinessStateInEnum caseStatusEnum = Objects.isNull(baseInfo.getBusinessStateIn())
                ? null
                : (BusinessStateInEnum) AfsEnumUtil.getEnum(baseInfo.getBusinessStateIn(),BusinessStateInEnum.class);
        Set<BusinessStateInEnum> resultBusinessStatus = new HashSet<>();
        resultBusinessStatus.add(BusinessStateInEnum.REPEAL);
        resultBusinessStatus.add(BusinessStateInEnum.REJECT);
        resultBusinessStatus.add(BusinessStateInEnum.APPROVED);
        resultBusinessStatus.add(BusinessStateInEnum.CONDITIONAL_APPROVE);
        resultBusinessStatus.add(BusinessStateInEnum.CANCEL_ADVICE_CONDITIONAL);
        return resultBusinessStatus.contains(caseStatusEnum);
    }


}
